package cn.tedu.loding.controller;


import cn.tedu.loding.pojo.dto.SkuSpecificationAddNewDTO;
import cn.tedu.loding.pojo.vo.SkuSpecificationListItemVO;
import cn.tedu.loding.service.ISkuSpecificationService;
import cn.tedu.loding.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "08.spuSpecification模块")
@Slf4j
@RestController
@Validated
@RequestMapping("/skuSpecification")
public class SkuSpecificationController {
    @Autowired
    private ISkuSpecificationService skuSpecificationService;

    public SkuSpecificationController() {
        log.debug("创建控制器对象：SkuSpecificationController");
    }

    @ApiOperation("添加SkuSpecification模板")
    @ApiOperationSupport(order = 1000)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid SkuSpecificationAddNewDTO skuSpecificationAddNewDTO){
        log.debug("开始处理【添加SkuSpecification】请求,参数为{}",skuSpecificationAddNewDTO);
        skuSpecificationService.addNew(skuSpecificationAddNewDTO);
        String message = "添加成功!";
        log.debug(message);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除SkuSpecification")
    @ApiOperationSupport(order = 1001)
    @ApiImplicitParam(name = "id", value = "SkuSpecificationId", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@Range(min = 1, message = "删除SkuSpecification失败，尝试删除的SkuSpecification的ID无效！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据id删除SkuSpecification】的请求，参数：{}", id);
        skuSpecificationService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询SkuSpecification列表")
    @ApiOperationSupport(order = 1002)
    @GetMapping("")
    public JsonResult<List<SkuSpecificationListItemVO>> list() {
        log.debug("开始处理【查询SkuSpecification列表】的请求，无参数");
        List<SkuSpecificationListItemVO> list = skuSpecificationService.list();
        return JsonResult.ok(list);

    }
}
